package com.umbrella.worker.web.controller.manager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.umbrella.worker.dto.NeedDO;
import com.umbrella.worker.dto.SupplierDO;
import com.umbrella.worker.query.NeedQuery;
import com.umbrella.worker.query.SupplierQuery;
import com.umbrella.worker.result.JsonResultDO;
import com.umbrella.worker.result.JsonResultSupport;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.INeedService;
import com.umbrella.worker.service.ISuppliersService;
import com.umbrella.worker.util.PageBeanUtil;

@Controller
@RequestMapping(value = "/mNeed")
public class ManagerNeedController {

	@Autowired
	private INeedService needService;
	
	@Autowired
	private ISuppliersService suppliersService;
	
	@RequestMapping(value = "/detail/{id}.html", method = RequestMethod.GET)
	public ModelAndView detail(
			ModelAndView mav,
			@PathVariable(value="id") Integer id
			) {
		SupplierQuery supplierQuery = new SupplierQuery();
		ResultDO result = suppliersService.list(supplierQuery);
		if(!result.isSuccess()) {
			mav.setViewName("error");
		} else {
			mav.addObject("SUPPLIER_LIST", result);
		}
		result = needService.get(id);
		if(result.isSuccess()) {
			mav.addObject("NEED_INFO", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("need/detail");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
	
	@RequestMapping(value = "/move/{id}/{supplierId}.json", method = RequestMethod.GET)
	public ModelAndView move(
			ModelAndView mav,
			@PathVariable(value="id") Integer id,
			@PathVariable(value="supplierId") Integer supplierId
			) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		SupplierDO supplierDO = null;
		ResultDO result = suppliersService.get(supplierId);
		if(!result.isSuccess()) {
			mav.setViewName("error");
		} else {
			supplierDO = (SupplierDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
		}
		NeedDO needDO = new NeedDO();
		needDO.setId(id);
		needDO.setwSupplierId(supplierId);
		needDO.setwSupplierName(supplierDO.getwSName());
		needDO.setwSupplierTel(supplierDO.getwSTelephone());
		
		result = needService.modify(needDO);
		if(result.isSuccess()) {
			jsonResultDO.setInfo("重新分配成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("重新分配失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}
	
	@RequestMapping(value = "/list/{supplierId}/{status}/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView list(
			ModelAndView mav,
			@PathVariable(value="supplierId") Integer supplierId,
			@PathVariable(value="status") Integer status,
			@PathVariable(value="pageNo") Integer pageNo,
			HttpServletRequest request
			) {
		System.out.println("mNeed");
		SupplierQuery supplierQuery = new SupplierQuery();
		ResultDO result = suppliersService.list(supplierQuery);
		if(!result.isSuccess()) {
			mav.setViewName("error");
		} else {
			mav.addObject("SUPPLIER_LIST", result);
		}
		NeedQuery query = new NeedQuery();
		if(supplierId != 0) { 
			query.setSupplierId(supplierId);
		}
		if(status != 0) {
			query.setStatus(status);
		}
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		
		result = needService.list(query);
		if(result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			SupplierQuery squery = new SupplierQuery();
			result = suppliersService.list(squery);
			mav.addObject("SUPPLIER_LIST", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);	
			mav.setViewName("manager/need/list");
		} else {
			mav.setViewName("error");
		}
		
		return mav;
	}
	
	
}
